#!/bin/bash

# 停车场管理系统启动脚本
# 包含数据库初始化和服务启动

echo "🚀 开始启动停车场管理系统..."

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查MySQL是否已安装
check_mysql() {
    if ! command -v mysql &> /dev/null; then
        echo -e "${RED}❌ MySQL未安装${NC}"
        echo "正在安装MySQL..."
        
        # 尝试使用Homebrew安装MySQL
        if command -v brew &> /dev/null; then
            echo "使用Homebrew安装MySQL..."
            brew install mysql@8.0
            brew link mysql@8.0 --force
            
            # 启动MySQL服务
            echo "启动MySQL服务..."
            brew services start mysql@8.0
            
            # 等待MySQL启动
            sleep 10
            
            # 设置root密码
            echo "设置MySQL root密码..."
            mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root123123'; FLUSH PRIVILEGES;"
            
        else
            echo -e "${RED}请先安装MySQL：${NC}"
            echo "1. 访问 https://dev.mysql.com/downloads/mysql/ 下载安装"
            echo "2. 或使用Homebrew: brew install mysql"
            exit 1
        fi
    else
        echo -e "${GREEN}✅ MySQL已安装${NC}"
    fi
}

# 检查MySQL服务状态
start_mysql_service() {
    echo "检查MySQL服务状态..."
    
    # 尝试多种方式启动MySQL
    if brew services list | grep -q "mysql.*started"; then
        echo -e "${GREEN}✅ MySQL服务已运行${NC}"
    else
        echo "正在启动MySQL服务..."
        
        # 尝试使用brew services
        if command -v brew &> /dev/null; then
            brew services start mysql@8.0 2>/dev/null || brew services start mysql 2>/dev/null
        fi
        
        # 尝试直接启动
        mysql.server start 2>/dev/null || sudo mysql.server start 2>/dev/null
        
        sleep 5
    fi
}

# 创建数据库和表
create_database() {
    echo "创建数据库和初始化数据..."
    
    # 检查数据库是否存在
    DB_EXISTS=$(mysql -u root -proot123123 -e "SHOW DATABASES LIKE 'park-management';" 2>/dev/null | grep -c "park-management")
    
    if [ $DB_EXISTS -eq 0 ]; then
        echo "创建数据库..."
        mysql -u root -proot123123 -e "CREATE DATABASE IF NOT EXISTS \`park-management\` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"
        
        # 检查SQL文件是否存在
        SQL_FILE="/Users/fish/Documents/vr/park-management/script/sql/ry_vue_5.X.sql"
        if [ -f "$SQL_FILE" ]; then
            echo "导入数据库初始化数据..."
            mysql -u root -proot123123 park-management < "$SQL_FILE"
            echo -e "${GREEN}✅ 数据库初始化完成${NC}"
        else
            echo -e "${YELLOW}⚠️  数据库初始化文件不存在，跳过数据导入${NC}"
        fi
    else
        echo -e "${GREEN}✅ 数据库已存在${NC}"
    fi
}

# 检查Redis是否已安装
check_redis() {
    if ! command -v redis-server &> /dev/null; then
        echo -e "${YELLOW}⚠️  Redis未安装，尝试安装...${NC}"
        
        if command -v brew &> /dev/null; then
            brew install redis
            brew services start redis
            echo -e "${GREEN}✅ Redis已安装并启动${NC}"
        else
            echo -e "${YELLOW}⚠️  请手动安装Redis：brew install redis${NC}"
        fi
    else
        echo -e "${GREEN}✅ Redis已安装${NC}"
        
        # 启动Redis
        if ! brew services list | grep -q "redis.*started"; then
            brew services start redis 2>/dev/null || redis-server &
        fi
    fi
}

# 主流程
main() {
    echo "========================================"
    echo "停车场管理系统启动向导"
    echo "========================================"
    
    # 检查环境
    check_mysql
    start_mysql_service
    create_database
    check_redis
    
    echo "========================================"
    echo "正在启动应用程序..."
    
    # 检查项目依赖
    echo "检查Maven依赖..."
    cd /Users/fish/Documents/vr/park-management
    mvn clean install -DskipTests
    
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}✅ 依赖安装完成${NC}"
        
        # 启动应用
        echo "启动Spring Boot应用..."
        cd /Users/fish/Documents/vr/park-management
        mvn spring-boot:run -Dspring-boot.run.profiles=dev -pl park-admin &
        
        # 等待应用启动
        sleep 15
        
        echo "========================================"
        echo -e "${GREEN}🎉 启动完成！${NC}"
        echo "访问地址: http://localhost:8080"
        echo "默认账号: admin"
        echo "默认密码: admin123"
        echo "========================================"
        
        # 打开浏览器
        if command -v open &> /dev/null; then
            sleep 5
            open http://localhost:8080
        fi
        
    else
        echo -e "${RED}❌ Maven构建失败，请检查错误信息${NC}"
        exit 1
    fi
}

# 执行主流程
main "$@"